/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.awt.HeadlessException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author PEKEM
 */
public final class MyTableModel extends AbstractTableModel {

    protected Vector vectorData;
    protected Vector vectorColumn;

    public MyTableModel(Vector data, Vector columnNames) {
        setDataVector(data, columnNames);
    }

    public void setDataVector(Vector dataVector, Vector columnIdentifiers) {
        this.vectorData = nonNullVector(dataVector);
        this.vectorColumn = nonNullVector(columnIdentifiers);
        justifyRows(0, getRowCount());
        fireTableStructureChanged();
    }

    public Vector getDataVector() {
        return vectorData;
    }

    private static Vector nonNullVector(Vector v) {
        return (v != null) ? v : new Vector();
    }

    private void justifyRows(int from, int to) {
        vectorData.setSize(getRowCount());
        for (int i = from; i < to; i++) {
            if (vectorData.elementAt(i) == null) {
                vectorData.setElementAt(new Vector(), i);
            }
            ((Vector) vectorData.elementAt(i)).setSize(getColumnCount());
        }
    }

    @Override
    public int getRowCount() {
        return vectorData.size();
    }

    @Override
    public int getColumnCount() {
        return vectorColumn.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Vector rowVector = (Vector) vectorData.elementAt(rowIndex);
        return rowVector.elementAt(columnIndex);
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        return "Số Lượng".equals(getColumnName(col));
    }

    @Override
    public String getColumnName(int column) {
        Object id = null;
        // This test is to cover the case when
        // getColumnCount has been subclassed by mistake ...
        if (column < vectorColumn.size() && (column >= 0)) {
            id = vectorColumn.elementAt(column);
        }
        return (id == null) ? super.getColumnName(column)
                : id.toString();
    }

    @Override
    public void setValueAt(Object aValue, int row, int column) {
        try {
            int soluong = Integer.parseInt(aValue.toString());
            if (soluong > 0 && soluong < 100) {
                Vector rowVector;
                rowVector = (Vector) vectorData.elementAt(row);
                rowVector.setElementAt(aValue, column);
                fireTableCellUpdated(row, column);
            } else {
                JOptionPane.showMessageDialog(null, "Số lượng phải lớn hơn 0 nhỏ hơn 100");
            }
        } catch (NumberFormatException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, "Bạn phải nhập số!");
        }

    }
}
